'use strict';

angular.module('app')
    .controller('maintainCreateController', [ '$scope', '$rootScope', '$state', '$timeout', 'toaster','$modal',
        function($scope, $rootScope, $state, $timeout, toaster,$modal) {
            $scope.title = '新增设备报障';
            $scope.record = {};
            $scope.save = function () {
                $scope.loading = true;
                $.ajax({//页面初始化数据请求
                    type: 'POST',
                    dataType: 'json',
                    contentType:'application/json;charset=UTF-8',
                    url : '/dac/dm/DmMaintain/add',
                    data: angular.toJson($scope.record)
                }).then(function(result) {
                	console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        toaster.clear('*');
                        toaster.pop('success','','保存成功！')
                        $timeout(function(){
                        	$state.go('main.dm.DmMaintain.list')
                        },2000)
                    }else{
                        toaster.clear("*");
                        toaster.pop('error','',result.msg);
                    }
                   $scope.$apply();
                });

            }
            //重置表单
            $scope.resetFrom = function(){
            	$scope.record = {};
            }
            //房屋地址模态框
            $scope.hoseAddress = function() {
                var modalInstance = $modal.open({
                    templateUrl : 'NowAddress.html',// script标签中定义的id
                    controller : 'bmBuildingListController',// modal对应的Controller
                    size: 'lg',
                    resolve : {
                        items : function() {
                            return '';
                        }
                    }
                });

                modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数

                });
                modalInstance.result.then(function(item) {
					$scope.record.buildingAddress = item.buildingAddress;
					$scope.record.deviceId = item.deviceId;
					$scope.record.locationId = item.buildingId;
                }, function(reason) {
                   	console.log(reason)
                });
            };
            //维修组模态框
            $scope.openGroup = function() {
                var modalInstance = $modal.open({
                    templateUrl : 'MainGroupList.html',// script标签中定义的id
                    controller : 'MainGroupController',// modal对应的Controller
                    size: 'lg',
                    resolve : {
                        items : function() {
                            return '';
                        }
                    }
                });

                modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数

                });
                modalInstance.result.then(function(item) {
					console.log(item);
					$scope.record.groupName = item.groupName;
					$scope.record.groupCode = item.groupCode;
                }, function(reason) {
                   	console.log(reason)
                });
            };
            
            app.controller('bmBuildingListController', function($scope, $rootScope, $modal,$modalInstance,items) {
            	
              	$scope.param ={};
				$scope.search = function() {
					$scope.loading = true;
					if($scope.pageFlag != 1) { //未点击翻页
						$scope.param.pageNum = 1; //将页码设置为1，即跳转回第一页
					} else { //点击翻页
						$scope.pageFlag = 2; //将开关值设置为2
					}
					$scope.param.cityId = $('#city').find("option:selected").data('code'); //获取value的值
					$scope.param.areaId = $('#areaId').find("option:selected").data('code'); //获取value的值
					$scope.param.streetId = $('#streetId').find("option:selected").data('code'); //获取value的值
					$scope.param.roadId = $('#roadId').find("option:selected").data('code'); //获取value的值
					$scope.param.communityId = $('#quarters').find("option:selected").data('code'); //获取value的值
					$scope.param.buildingNo = $('#buildingNo').val();
					$scope.param.deviceCode = $('#deviceCode').val();
					$scope.param.deviceName = $('#deviceName').val();
					$scope.param.personId = $('#personId').val();
					$scope.param.buildName = $('#buildName').val();
					$.ajax({
						type: 'POST',
						dataType: 'json',
						contentType: 'application/json;charset=UTF-8',
						url: '/dac/authm/build/list',
						data: angular.toJson($scope.param)
					}).then(function(result) {
						$scope.loading = false;
						if(result.httpCode == 200) {
							console.log(result)
							$scope.pageInfo = result;
						} else {
							$scope.msg = result.msg;
						}
						$scope.$apply();
					});
				}
		        $scope.search();
	            //7级地址下拉数据
				//市
				$scope.getaddress = function() {
					$scope.param = {
						parentCode: 440000 //广东省
					};
					$.ajax({
						type: 'PUT',
						dataType: 'json',
						contentType: 'application/json;charset=UTF-8',
						url: '/dic/queryDicListByParentId',
						data: angular.toJson($scope.param)
					}).then(function(result) {
						$scope.city = result.data;
						console.log(result);
					})
				}
				$scope.getaddress();
				//区
				$scope.getarea = function() {
					var parentCode = $('#city').find("option:selected").data('code'); //获取value的值
					console.log(parentCode);
					$scope.param = {
						parentCode: parentCode //广州市区级
					};
					$.ajax({
						type: 'PUT',
						dataType: 'json',
						contentType: 'application/json;charset=UTF-8',
						url: '/dic/queryDicListByParentId',
						data: angular.toJson($scope.param)
					}).then(function(result) {
						$scope.area = result.data;
						console.log(result);
					})
				}
				//乡镇/街道
				$scope.getStree = function() {
					var parentCode = $('#area').find("option:selected").data('code'); //获取value的值
					console.log(parentCode);
					$scope.param = {
						parentCode: parentCode
					};
					$.ajax({
						type: 'PUT',
						dataType: 'json',
						contentType: 'application/json;charset=UTF-8',
						url: '/dic/queryDicListByParentId',
						data: angular.toJson($scope.param)
					}).then(function(result) {
						$scope.stree = result.data;
						console.log(result);
					})
				}
				//路
				$scope.getRoad = function() {
					var parentCode = $('#street').find("option:selected").data('code'); //获取value的值;
					console.log(parentCode)
					$scope.param = {
						parentCode: parentCode
					};
					$.ajax({
						type: 'PUT',
						dataType: 'json',
						contentType: 'application/json;charset=UTF-8',
						url: '/dic/queryDicListByParentId',
						data: angular.toJson($scope.param)
					}).then(function(result) {
						$scope.roud = result.data;
						console.log(result);
					})
				}
				//小区
				$scope.getQuarters = function() {
					var parentCode = $('#road').find("option:selected").data('code'); //获取value的值;
					$scope.param = {
						parentCode: parentCode
					};
					$.ajax({
						type: 'PUT',
						dataType: 'json',
						contentType: 'application/json;charset=UTF-8',
						url: '/dic/queryDicListByParentId',
						data: angular.toJson($scope.param)
					}).then(function(result) {
						$scope.quarters = result.data;
						console.log(result);
					})
				}
				//房号
				$scope.getHouseNo = function() {
					var parentCode = $('#quarters').find("option:selected").data('code'); //获取value的值;
					$scope.param = {
						parentCode: parentCode
					};
					$.ajax({
						type: 'PUT',
						dataType: 'json',
						contentType: 'application/json;charset=UTF-8',
						url: '/dic/queryDicListByParentId',
						data: angular.toJson($scope.param)
					}).then(function(result) {
						$scope.houseNo = result.data;
						console.log(result);
					})
				}
		        //选择按钮操作
				$scope.selectBuilding = function(buildingId, buildingAddress, deviceId) {
					$scope.buildingId = {
						buildingId: buildingId,
						buildingAddress: buildingAddress,
						deviceId: deviceId
					}
					$modalInstance.close($scope.buildingId);
				};
				$scope.cancel = function() {
					$modalInstance.dismiss('cancel');
				};
				// 翻页
				$scope.pagination = function(page) {
					$scope.param.pageNum = page;
					$scope.pageFlag = 1; //将开关值设置为1
					$scope.search();
				};
		    })
            //选择组模态框控制器
	        app.controller('MainGroupController', function($scope, $rootScope, $modal,$modalInstance,items) {
              	$scope.param ={};
                $scope.search = function () {
                    $scope.loading = true;
                    if($scope.pageFlag != 1){//未点击翻页
                        $scope.param.pageNum=1;//将页码设置为1，即跳转回第一页
                    }else{//点击翻页
                        $scope.pageFlag = 2;//将开关值设置为2
                    }
                    $.ajax({
                        type: 'POST',
                        dataType: 'json',
                        contentType:'application/json;charset=UTF-8',
                        url : '/dac/dm/DmMaintain/groupList',
                        data: angular.toJson($scope.param)
                    }).then(function(result) {
                        $scope.loading = false;
                        if (result.httpCode == 200) {
                            $scope.pageInfo = result;
                        } else {
                            toaster.clear('*');
                            toaster.pop('error', '', result.msg);
                        }
                        $scope.$apply();
                    });
                }
                $scope.search();
	            
	            $scope.selectGroup = function(groupName,groupCode){
            		var item = {
	            		groupName:groupName,
	            		groupCode:groupCode
            		};
            		
            		$modalInstance.close(item);
	            }
	
                $scope.cancel = function() {
                    $modalInstance.dismiss('cancel');
                };

                // 翻页
                $scope.pagination = function (page) {
                    $scope.param.pageNum=page;
                    $scope.pageFlag = 1;//将开关值设置为1
                    $scope.search();
                };
            })

    } ]);
        
	